package com.spzx.manager.controller;

import com.github.pagehelper.PageInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.OperatorType;
import com.spzx.manager.service.SysRoleService;
import com.spzx.model.dto.system.SysRoleDto;
import com.spzx.model.entity.system.SysRole;
import com.spzx.model.vo.common.Result;
import com.spzx.model.vo.common.ResultCodeEnum;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 角色添加
 * Project: spzx-parent
 * Package: com.spzx.manager.controller
 * Version: 1.0
 * <p>
 * Created by  wangjiaxin  on 2023/12/11 22:04
 */
@Tag(name = "角色接口")
@RestController
@RequestMapping(value = "/admin/system/sysRole")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    /**
     * 角色列表查找
     *
     * @param sysRoleDto 要查找的实体
     * @param pageNum    查找的页数
     * @param pageSize   一页显示的个数
     * @return
     */
    @PostMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysRole>> findByPage(@PathVariable(value = "pageNum") Integer pageNum,
                                                @PathVariable(value = "pageSize") Integer pageSize,
                                                @RequestBody SysRoleDto sysRoleDto) {
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(sysRoleDto, pageNum, pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 角色添加
     *
     * @param SysRole
     * @return
     */
    @Log(title = "角色管理：添加", operatorType = OperatorType.MANAGE, businessType = 1)
    @PostMapping(value = "/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole SysRole) {
        sysRoleService.saveSysRole(SysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 角色修改
     *
     * @param sysRole
     * @return
     */
    @PutMapping(value = "/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole) {
        sysRoleService.updateSysRole(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 角色删除
     * 逻辑删除，修改字段
     *
     * @param roleId
     * @return
     */
    @DeleteMapping(value = "/deleteById/{roleId}")
    public Result deleteById(@PathVariable(value = "roleId") Long roleId) {
        sysRoleService.deleteById(roleId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    /**
     * 查询所有用户角色
     *
     * @return
     */
    @GetMapping(value = "/findAllRoles/{userId}")
    public Result<Map<String, Object>> findAllRoles(@PathVariable(value = "userId") Long userId) {
        Map<String, Object> resultMap = sysRoleService.findAllRoles(userId);
        return Result.build(resultMap, ResultCodeEnum.SUCCESS);
    }
}
